package com.local.goods.service.impl;

import com.local.common.constants.CommonConstants;
import com.local.common.constants.MallCategoryLevelEnum;
import com.local.common.util.PageResultUtil;
import com.local.goods.mapper.GoodsCategoryMapper;
import com.local.goods.pojo.GoodsCategory;
import com.local.goods.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    
    @Override
    public PageResultUtil<GoodsCategory> findGoodsCategoryList(Integer parentId, Byte categoryLevel, Integer page, Integer limit, String sort, String order, Integer isDeleted) {
        Integer start = (page - 1) * limit;
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.findGoodsCategoryList(parentId, categoryLevel, start, limit, sort, order, isDeleted);
        Integer totalGoodsCategories = goodsCategoryMapper.getTotalGoodsCategories(parentId, categoryLevel, isDeleted);
        PageResultUtil<GoodsCategory> pageResultUtil = new PageResultUtil<>(goodsCategoryList, totalGoodsCategories, limit, page);

        return pageResultUtil;
    }

    /**
     * 新增
     * @param goodsCategory
     * @return
     */
    @Override
    public Integer save(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.save(goodsCategory);
    }

    /**
     * 修改
     * @param goodsCategory
     * @return
     */
    @Override
    public Integer update(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.update(goodsCategory);
    }

    /**
     * 批量删除
     * @param ids
     * @param updateUserId
     * @param updateTime
     * @return
     */
    @Override
    public Integer deleteByIds(Integer[] ids, Integer updateUserId, String updateTime) {
        return goodsCategoryMapper.deleteByIds(ids, updateUserId, updateTime);
    }

    /**
     * 通过 parent_id 查询分类列表
     * @param parentIds
     * @param categoryLevel
     * @param number
     * @return
     */
    @Override
    public List<GoodsCategory> selectByLevelAndParentIdsAndNumber(List<Integer> parentIds, Integer categoryLevel, Integer number) {
        return goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(parentIds, categoryLevel, number);
    }

    /**
     * 通过id查询
     * @param categoryId
     * @return
     */
    @Override
    public GoodsCategory selectById(Integer categoryId) {
        return goodsCategoryMapper.selectById(categoryId);
    }


    /**
     * 得到首页的商品分类列表
     * @return
     */
    @Override
    public List<GoodsCategory> getIndexCategoryList() {
        //一级分类列表
        List<GoodsCategory> goodsCategoriesLevelOne = goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(0), MallCategoryLevelEnum.LEVEL_ONE.getLevel(), CommonConstants.INDEX_CATEGORY_NUMBER);
        if (goodsCategoriesLevelOne.size() > 0) {
            for (GoodsCategory goodsCategoryLevelOne : goodsCategoriesLevelOne) {
                //二级分类列表
                List<GoodsCategory> goodsCategoriesLevelTwo = goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(goodsCategoryLevelOne.getCategoryId()), MallCategoryLevelEnum.LEVEL_TWO.getLevel(), 0);
                if (goodsCategoriesLevelTwo.size() > 0) {
                    for (GoodsCategory goodsCategoryLevelTwo : goodsCategoriesLevelTwo) {
                        //三级分类列表
                        List<GoodsCategory> goodsCategoriesLevelThree = goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(goodsCategoryLevelTwo.getCategoryId()), MallCategoryLevelEnum.LEVEL_THREE.getLevel(), 0);

                        goodsCategoryLevelTwo.setChildGoodsCategories(goodsCategoriesLevelThree);
                    }
                }

                goodsCategoryLevelOne.setChildGoodsCategories(goodsCategoriesLevelTwo);
            }
        }

        return goodsCategoriesLevelOne;
    }


    /**
     * 得到商品分类页的分类信息
     * @param categoryId
     * @return
     */
    @Override
    public GoodsCategory getSearchCategory(Integer categoryId) {
        GoodsCategory goodsCategory = goodsCategoryMapper.selectById(categoryId);
        if (goodsCategory.getCategoryLevel() == MallCategoryLevelEnum.LEVEL_THREE.getLevel().byteValue()) {
            GoodsCategory parentGoodsCategory = goodsCategoryMapper.selectById(goodsCategory.getParentId());
            List<GoodsCategory> childGoodsCategoryList = goodsCategoryMapper.findGoodsCategoryList(goodsCategory.getParentId(), MallCategoryLevelEnum.LEVEL_THREE.getLevel().byteValue(), null, null, "category_rank", "asc", 0);
            parentGoodsCategory.setChildGoodsCategories(childGoodsCategoryList);
            parentGoodsCategory.setCurrentCategoryName(goodsCategory.getCategoryName());

            return parentGoodsCategory;
        }




        return null;
    }


}
